package com.cychat.mall.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Auther: 刘俊  <br/>
 * Date: 2025-11-8:下午 3:05  <br/>
 * Description: RabbitMQ 配置类
 */
@Configuration
public class RabbitMQConfig {
    public static final String ORDER_DELAY_EXCHANGE = "order.delay.exchange";
    public static final String ORDER_DELAY_QUEUE = "order.delay.queue";
    public static final String ORDER_DELAY_ROUTING_KEY = "order.delay";

    // 延迟交换机（Direct）
    @Bean
    public DirectExchange orderDelayExchange() {
        return new DirectExchange(ORDER_DELAY_EXCHANGE);
    }

    // 延迟队列（设置 TTL 为 10 秒）
    @Bean
    public Queue orderDelayQueue() {
        return QueueBuilder.durable(ORDER_DELAY_QUEUE)
                .withArgument("x-message-ttl", 15 * 60 * 1000) // 15分钟
                .withArgument("x-dead-letter-exchange", "order.release.exchange")
                .withArgument("x-dead-letter-routing-key", "order.release")
                .build();
    }

    @Bean
    public Binding orderDelayBinding() {
        return BindingBuilder.bind(orderDelayQueue())
                .to(orderDelayExchange())
                .with(ORDER_DELAY_ROUTING_KEY);
    }

    // 死信交换机（用于超时处理）
    @Bean
    public DirectExchange orderReleaseExchange() {
        return new DirectExchange("order.release.exchange");
    }

    @Bean
    public Queue orderReleaseQueue() {
        return QueueBuilder.durable("order.release.queue").build();
    }

    @Bean
    public Binding orderReleaseBinding() {
        return BindingBuilder.bind(orderReleaseQueue())
                .to(orderReleaseExchange())
                .with("order.release");
    }
}
